package com.cn.example;

import com.cn.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;

@Controller
public class XmlController {

    //返回的数据时html
    @PostMapping("/processXml")
    public String processXml(@RequestBody String xmlData, Model model) {
        try {
            // 解析 XML 数据
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new org.xml.sax.InputSource(new StringReader(xmlData)));

            // 获取用户信息
            String name = doc.getElementsByTagName("name").item(0).getTextContent();
            String age = doc.getElementsByTagName("age").item(0).getTextContent();
            System.out.println("name:"+name);
            System.out.println("age:"+age);

            // 处理业务逻辑
            String status = Integer.parseInt(age) > 18 ? "Adult" : "Minor";

            // 构建 XML 响应
            Document responseDoc = builder.newDocument();
            Element responseElement = responseDoc.createElement("response");
            responseDoc.appendChild(responseElement);

            Element userElement = responseDoc.createElement("user");
            responseElement.appendChild(userElement);

            Element nameElement = responseDoc.createElement("name");
            nameElement.setTextContent(name);
            userElement.appendChild(nameElement);

            Element statusElement = responseDoc.createElement("status");
            statusElement.setTextContent(status);
            userElement.appendChild(statusElement);

            // 将 XML 转换为字符串
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(responseDoc), new StreamResult(writer));

            //String responseXml = writer.toString();
            User user = new User();
            user.setName("刘婧伊");
            user.setAge(18);

            // 将数据添加到模型中
            model.addAttribute("user", user);

            return "result"; // 渲染 result.html

        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "Error processing XML");
            return "error"; // 错误页面
        }
    }


    // 返回的数据时xml
    @PostMapping("/processXml2")
    public String processXml2(@RequestBody String xmlData, Model model) {
        try {
            // 解析 XML 数据
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new org.xml.sax.InputSource(new StringReader(xmlData)));

            // 获取用户信息
            String name = doc.getElementsByTagName("name").item(0).getTextContent();
            String age = doc.getElementsByTagName("age").item(0).getTextContent();
            System.out.println("name:"+name);
            System.out.println("age:"+age);

            // 处理业务逻辑
            String status = Integer.parseInt(age) > 18 ? "Adult" : "Minor";

            //返回的数据模型
            User user = new User();
            user.setName("刘婧伊");
            user.setAge(18);

            // 将数据添加到模型中
            model.addAttribute("user", user);

            return "result2"; // 渲染 result2.xml

        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "Error processing XML");
            return "error"; // 错误页面
        }
    }
}